//
//  UserEntity.m
//  CPIC
//
//  Created by kane on 14-5-4.
//  Copyright (c) 2014年 zhangbo. All rights reserved.
//

#import "UserEntity.h"
#import <CommonCrypto/CommonDigest.h>

@implementation UserEntity

@synthesize MobilePhone;
@synthesize NickName;
@synthesize Password;
@synthesize PictureUrl;
@synthesize Email;

+ (UserEntity *)sharedUser
{
    static UserEntity *_sharedUser = nil;
    static dispatch_once_t onceUser;
    dispatch_once(&onceUser, ^{
        _sharedUser = [[UserEntity alloc] init];
        _sharedUser.SessionKey = @"";
    });
    return  _sharedUser;
}

- (void)login:(NSString *)username password:(NSString *)password
{
        
    [[ActivityView shareActivityView] showWithString:@"正在登录..."];
     NSDictionary *params = @{@"mobile":username,
                              @"password":password,
                              @"login_type":@"1"};
    NSLog(@"params : %@",params);
    [WebService loginWithParams:params WithBlock:^(id result, NSError *error) {
        [[ActivityView shareActivityView] dismiss];
        if (error) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else{
            if ([result[@"result"] intValue] == 1) {
                self.MobilePhone = username;
                self.NickName = result[@"user"][@"name"];
                
                [[NSUserDefaults standardUserDefaults] setObject:username forKey:@"lastUserName"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationLoginSuccess object:@1];

            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:result[@"message"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        }
    }];
}

- (void)loginSMS:(NSString *)username
{
    [[ActivityView shareActivityView] showWithString:@"正在登录..."];
    NSDictionary *params = @{@"mobile":username,
                             @"password":@"",
                             @"login_type":@"2"};
    NSLog(@"params : %@",params);
    [WebService loginWithParams:params WithBlock:^(id result, NSError *error) {
        [[ActivityView shareActivityView] dismiss];
        if (error) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
        }else{
            if ([result[@"result"] intValue] == 1) {
                self.MobilePhone = username;
                self.NickName = result[@"user"][@"name"];
                
                [[NSUserDefaults standardUserDefaults] setObject:username forKey:@"lastUserName"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationLoginSuccess object:@1];
                
            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:result[@"message"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        }
    }];
}

- (void)logout
{
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"lastUserName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationLogoutSuccess object:nil];
    return;
    NSDictionary *params = @{@"username":self.MobilePhone,
                             @"client":@"ios",
                             @"key":self.SessionKey};
    [WebService logoutWithParams:params WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([[result objectForKey:@"code"] intValue] == 0) {
                NSString *msg = [[result objectForKey:@"datas"] objectForKey:@"error"];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                return;
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
}

- (NSString *)getUserID
{
    return [UserEntity sharedUser].loginSuccess ? [UserEntity sharedUser].UserID : DeviceUUIDString;
}

- (void)findAddress:(BOOL)synAddress
{
    if (self.isLoading) {
        return;
    }
    if (self.CityName && !synAddress) {
        return;
    }
    
    self.isLoading = YES;
    NSDictionary *params = @{@"lng":self.Longitude,@"lat":self.Latitude};
    
    [WebService getAddress:params WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([result[@"status"] isEqualToString:@"OK"]) {
                self.CityName = result[@"result"][@"addressComponent"][@"city"];
                NSArray *cityAry = [[Database sharedManager] searchCitys:self.CityName];
                if (cityAry.count > 0) {
                    NSDictionary *LBSCity = cityAry[0];
                    self.AreaID = LBSCity[@"id"];
                    self.AreaName = LBSCity[@"areaname"];
                    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationCityChanged object:nil];

                    [[NSUserDefaults standardUserDefaults] setObject:LBSCity forKey:@"LBSCity"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                }
            }
            self.isLoading = NO;
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
}

@end
